package com.example.shaoying.yuejian.activity;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;


import com.example.shaoying.yuejian.R;
import com.example.shaoying.yuejian.model.UserModel;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;


public class UpdateUserActivity extends Activity {


    private EditText user_name;
    private EditText user_password;
    private Button user_change1;
    private Button user_change2;
    String name;
    String password;
    private SharedPreferences sp;
    private String userId;
    private String sessionid;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.person);
        sp = getSharedPreferences("login_uid", Context.MODE_PRIVATE);
        sessionid=sp.getString("sessionId","");
        userId=sp.getString("userId","");

        user_change1= (Button) findViewById(R.id.user_change_button1);
        user_change2= (Button) findViewById(R.id.user_change_button2);
        user_name= (EditText) findViewById(R.id.user_change_name);
        user_password= (EditText) findViewById(R.id.user_change_password);
        user_change1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                name=user_name.getText().toString();
                if (name.equals("")){
                    Toast.makeText(UpdateUserActivity.this,"你的用户名不能为空",Toast.LENGTH_LONG).show();
                }else {
                    UserModel model=new UserModel();
                   Call call= model.getUserChangeName(userId,name);
                    call.enqueue(new Callback<Integer>() {
                        @Override
                        public void onResponse(Call<Integer> call, Response<Integer> response) {
                            if (response.body()==1){
                                Toast.makeText(UpdateUserActivity.this,"用户名修改成功",Toast.LENGTH_LONG).show();
                            }else {
                                Toast.makeText(UpdateUserActivity.this,"用户名修改失败",Toast.LENGTH_LONG).show();
                            }
                        }

                        @Override
                        public void onFailure(Call<Integer> call, Throwable t) {

                        }
                    });
                }
            }
        });

        user_change2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                password=user_password.getText().toString();
                if(password.equals("")){
                    Toast.makeText(UpdateUserActivity.this,"您输入的密码为空",Toast.LENGTH_LONG).show();
                }else {
                    Toast.makeText(UpdateUserActivity.this,userId+"----"+password,Toast.LENGTH_LONG).show();
                    UserModel model=new UserModel();
                    Call call= model.getUserChangePassword(userId,password);
                    call.enqueue(new Callback<Integer>() {
                        @Override
                        public void onResponse(Call<Integer> call, Response<Integer> response) {
                            Toast.makeText(UpdateUserActivity.this,response.body().toString(),Toast.LENGTH_LONG).show();
                            if (response.body()==1){
                                Toast.makeText(UpdateUserActivity.this,"密码修改成功",Toast.LENGTH_LONG).show();
                            }else {
                                Toast.makeText(UpdateUserActivity.this,"密码修改失败",Toast.LENGTH_LONG).show();
                            }
                        }

                        @Override
                        public void onFailure(Call<Integer> call, Throwable t) {
                            Toast.makeText(UpdateUserActivity.this,"----",Toast.LENGTH_LONG).show();
                        }
                    });
                }
            }
        });
    }
}
